# -*- coding: UTF-8 -*-

import pymysql
import requests
import json
import threading
import time
from datetime import datetime


# 接口地址
API = "http://5xsy.com/apisample.php"

'''
先判断当前是否周五下午，是则表示要取交割数据
'''
now = time.localtime(time.time())
print(now.tm_wday)
if now.tm_wday==5 and now.tm_hour>=16 and now.tm_hour<=17:
	isDone = True
	count = 0
	while isDone:# 若未交割完成，则循环查询
		# 获取合约状态
		r = requests.get(API)
		#print(r.text)
		dataObj = json.loads(r.text)
		items = dataObj['data']
		
		for item in items:
			isDone = item['contract_status']==1
			if not isDone:
				break
		isDone = not isDone
		count = count + 1
		if count>=4:
			print("请求数据超时!")
			exit()  # 防止超时，永久循环
		print("交割未完成...%s"%count)
		time.sleep(10)

print("正常日获取数据!")